parse: Add function global function parse_date.
authoroliskoli <oliskoli>
Sun, 17 Aug 2008 20:52:22 +0000 (20:52 +0000)
committeroliskoli <oliskoli>
Sun, 17 Aug 2008 20:52:22 +0000 (20:52 +0000)
defs.h
parse.c

diff --git a/defs.h b/defs.h
index c4ded48de4a9ac397d7e8efb7bdc26c8e9a346d9..03c2ec2dfdee3a4ba173444a1ecd1cca26bb7574 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -958,6 +958,7 @@ int parse_coordinates(const char *str, int datum, const grid_type grid,
        double *latitude, double *longitude, const char *module);
 int parse_distance(const char *str, double *val, double scale, const char *module);
 int parse_speed(const char *str, double *val, const double scale, const char *module);
+time_t parse_date(const char *str, const char *format, const char *module);
 
 /*
  *  From util_crc.c
diff --git a/parse.c b/parse.c
index e6ee8b44a19520f2c97eafe397d23be4eb222441..d0217c4a4f6d77ddfdfac1f1c5619ce73cad097d 100644 (file)
--- a/parse.c
+++ b/parse.c
 
  */
 
-#include "defs.h"
-#include "jeeps/gpsmath.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <errno.h>
 #include <time.h>
 
+#include "defs.h"
+#include "jeeps/gpsmath.h"
+#include "strptime.h"
+
 /*
  * parse_distance:
  *
@@ -235,3 +237,54 @@ parse_coordinates(const char *str, int datum, const grid_type grid,
                
        return result;
 }
+
+
+time_t
+parse_date(const char *str, const char *format, const char *module)
+{
+       struct tm tm;
+
+       memset(&tm, 0, sizeof(tm));
+
+       if (format) {
+               char *cx = strptime(str, format, &tm);
+               if ((cx != NULL) && (*cx != '\0'))
+                       fatal("%s: Could not parse date string (%s).\n", module, str);
+       }
+       else {
+               int p1, p2, p3, ct;
+               char sep[2];
+
+               ct = sscanf(str, "%d%1[-.//]%d%1[-.//]%d", &p1, sep, &p2, sep, &p3);
+               if (ct != 5)
+                       fatal("%s: Could not parse date string (%s).\n", module, str);
+
+               if ((p1 > 99) || (sep[0] == '-')) { /* Y-M-D (iso like) */
+                       tm.tm_year = p1;
+                       tm.tm_mon = p2;
+                       tm.tm_mday = p3;
+               }
+               else if (sep[0] == '.') {       /* Germany and any other countries */
+                       tm.tm_mday = p1;        /* have a fixed D.M.Y format */
+                       tm.tm_mon = p2;
+                       tm.tm_year = p3;
+               }
+               else {
+                       tm.tm_mday = p2;
+                       tm.tm_mon = p1;
+                       tm.tm_year = p3;
+               }
+               if ((p1 < 100) && (p2 < 100) && (p3 < 100)) {
+                       if (tm.tm_year < 70) tm.tm_year += 2000;
+                       else tm.tm_year += 1900;
+               }
+               /* some low-level checks */
+               if ((tm.tm_mon > 12) || (tm.tm_mon < 1) || (tm.tm_mday > 31) || (tm.tm_mday < 1))
+                       fatal("%s: Could not parse date string (%s).\n", module, str);
+
+               tm.tm_year -= 1900;
+               tm.tm_mon -= 1;
+       }
+       
+       return mkgmtime(&tm);
+}